// Copyright (c) Microsoft Corporation. 
// Licensed under the MIT license.

#pragma once

#include "CoreMinimal.h"
#include "Widgets/DeclarativeSyntaxSupport.h"
#include "Widgets/SCompoundWidget.h"
#include "Widgets/Views/SListView.h"
#include "Delegates/IDelegateInstance.h"

class UMorphTarget;
class ULODRecipe;
class ITableRow;
class STableViewBase;
class SAllowMorphTargetsWidget;
class SHeaderRow;
class SVerticalBox;

class FMorpTargetItem
{
public:
	FMorpTargetItem() = default;
	FMorpTargetItem(UMorphTarget* InMorphTarget, ULODRecipe* InLODRecipeBeingEdited);

	TWeakObjectPtr<UMorphTarget> MorphTarget;
	TWeakObjectPtr<ULODRecipe> LODRecipeBeingEdited;
};
typedef TSharedPtr<FMorpTargetItem> FMorpTargetItemPtr;

class SAllowMorphTargetsList : public SCompoundWidget
{
	SLATE_BEGIN_ARGS(SAllowMorphTargetsList)
	{
	}

	SLATE_END_ARGS()

	/** Widget constructor */
	void Construct(const FArguments& Args, const TSharedRef<class SAllowMorphTargetsWidget> Owner);
	~SAllowMorphTargetsList();

	void Refresh();

private:
	void RebuildMorphTargetList();
	EActiveTimerReturnType TriggerRebuildMorphTargetList(double InCurrentTime, float InDeltaTime);

	TSharedRef<ITableRow> OnGenerateRow(FMorpTargetItemPtr Item, const TSharedRef<STableViewBase>& OwnerTable);

	void OnPropertyChanged(UObject* ObjectBeingModified, FPropertyChangedEvent& PropertyChangedEvent);
	void RefreshColumns(); 
	void RefreshMainContent();
private:
	TWeakPtr<SAllowMorphTargetsWidget> OwnerWidget;

	TSharedPtr<SListView<FMorpTargetItemPtr>> MorphTargetListView;
	TArray<FMorpTargetItemPtr> MorphTargetListItems;

	TSharedPtr<SHeaderRow> ListHeaders;
	TSharedPtr<SVerticalBox> MainContent;

	/** Whether the active timer to refresh the list is registered */
	bool bIsActiveTimerRegistered;

	FDelegateHandle OnPropertyChangedDelegateHandle;
};